
import fs from "fs";
// 获取文件的后缀
export const getUploadFileExt = (name: string) => {
  let ext = name.split(".");
  return ext[ext.length - 1];
};

/**
 * @description 判断文件夹是否存在 如果不存在则创建文件夹
 */
export const checkDirExist = (p: string) => {
  if (!fs.existsSync(p)) {
    fs.mkdirSync(p);
  }
};
/***
 * 每日上传文件区分
 */
export const getUploadDirName = () => {
  const date = new Date();
  let month: string | number = date.getMonth() + 1;
  month = month.toString().length > 1 ? month : `0${month}`;
  const dir = `${date.getFullYear()}${month}${date.getDate()}`;
  return dir;
};

/***
 * 生成文件名
 */
export const getUploadFileName = (ext:string) => {
  const date = new Date();
  let month: string | number = date.getMonth() + 1;
  month = month.toString().length > 1 ? month : `0${month}`;
  const file = `${date.getFullYear()}${month}${date.getDate()}_${Math.floor(
    Math.random() * 10000
  )}.${ext}`;
  return file;
};
